p = 2^127 - 1 (a Mersenne prime).
The prime field F_p
Field definition
The field is defined by the prime:Using a Mersenne prime enables efficient modular reduction using bit shifts and additions instead of expensive division.
Field elements
Field elements are represented as 128-bit integers with the top bit always zero:include/pvac/core/field.hpp:17-20:
The
hi field uses only 63 bits. Values are stored in the range [0, p-1].Field operations
Addition
Addition with modular reduction:include/pvac/core/field.hpp:50-56.
Steps:
- Add low words with carry
- Add high words with propagated carry
- Reduce modulo p using
fp_from_words
Subtraction
Subtraction via negation:p - a:
include/pvac/core/field.hpp:58-67.
Multiplication
Multiplication uses 128×128 → 256-bit widening multiplication:include/pvac/core/field.hpp:209-213.
Implementation:
- Uses platform-specific optimizations (x86 assembly, MSVC intrinsics, or portable 128-bit)
- Computes full 256-bit product
- Reduces modulo
2^127 - 1efficiently
Reduction modulo p
The reduction algorithm exploits the Mersenne prime structure:include/pvac/core/field.hpp:179-207.
Key insight: Since 2^127 ≡ 1 (mod p), we can reduce by adding the high bits to the low bits.
Inversion
Inversion uses Fermat’s Little Theorem:a^(p-1) ≡ 1 (mod p), so a^(-1) = a^(p-2).
include/pvac/core/field.hpp:229-269.
Why this field?
Advantages of F_(2^127 - 1)
- Mersenne prime: Fast reduction using bit operations
- Large enough: 127 bits provides ample space for computations
- Multiplicative group:
p-1 = 2^127 - 2is divisible by many small factors - No NTT constraints: Unlike RLWE schemes, no need for NTT-friendly primes
Multiplicative group structure
The multiplicative group has orderp - 1 = 2^127 - 2.
Key generation requires finding a generator g of a subgroup of order B = 337:
From include/pvac/core/types.hpp:40:
The parameter
B is chosen so that B | (p-1), enabling efficient subgroup operations. The value 337 is a prime that divides 2^127 - 2.Vector operations
For batching (multi-slot encryption), operations extend element-wise:include/pvac/ops/encrypt.hpp:150-154.
Performance
Field operation timings on modern x86-64 CPUs:Code example
Next steps
Encryption scheme
Learn how field elements are encrypted
Homomorphic operations
Understand operations on encrypted data